﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Xml;


namespace ClassLibrary
{
    /* class for describing data about single characteristic */
    public class CharacteristicDescription
    {
        public int id { set; get; }
        public string name { set; get; }
        public int max_degree { set; get; }
        public CharacteristicDescription(int characteristic_id, string characteristic_name, int characteristic_max_degree)
        {
            id = characteristic_id;
            name = characteristic_name;
            max_degree = characteristic_max_degree;
        }
    }

    /* class for describing data about single characteristic
     * with addidive fields such as characteristic's degree 
     *     
     */
    public class Characteristic
    {
        public int id { set; get; }
        public string name { set; get; }
        public int degree { set; get; }
        public Characteristic(int characteristic_id, string characteristic_name)
        {
            id = characteristic_id;
            name = characteristic_name;
            degree = 0;
        }
    }


    /*
     * class for holding data about
     * phenotype characteristics 
     */
    public class PhenotypeTable
    {
        public string path { set; get; }
        public string name { set; get; }
        public int characteristics_amount;
        public Dictionary<string,CharacteristicDescription> characteristic_table = new Dictionary<string,CharacteristicDescription>();


        /* Constructor
         * Args: path to the file where structure of phenotype is described
         * Do: creates massive for data of different charecteristics
        */
        public PhenotypeTable(string path_to_phenotype_table)
        {
            path = path_to_phenotype_table;

            // open XML file            
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(File.ReadAllText(path_to_phenotype_table));

            // reading phenotype_table's attributes            
            name = doc.DocumentElement.GetAttribute("Name");
            characteristics_amount = Convert.ToInt32(doc.DocumentElement.GetAttribute("CharacteristicsAmount"));


            // reading all data about characteristics
            if (characteristics_amount != 0)
            {
                // reading "characteristics" node
                int i = -1;
                int characteristic_id;
                string characteristic_name;
                int characteristic_max_degree;
                foreach (XmlNode xml_node in doc.GetElementsByTagName("characteristic"))
                {
                    i++;
                    characteristic_id = Convert.ToInt32(xml_node.Attributes[0].Value);
                    characteristic_name = xml_node.Attributes[1].Value;
                    characteristic_max_degree = Convert.ToInt32(xml_node.Attributes[2].Value);
                    characteristic_table.Add(characteristic_name, new CharacteristicDescription(characteristic_id, characteristic_name, characteristic_max_degree));                  
                }
            }          
        }
    }
}